package com.gzy.mylog.controller;

import com.gzy.mylog.controller.paramer.LoginPara;
import com.gzy.mylog.controller.paramer.UserUpdatePara;
import com.gzy.mylog.core.PageList;
import com.gzy.mylog.core.R;
import com.gzy.mylog.model.Log;
import com.gzy.mylog.model.User;
import com.gzy.mylog.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@CrossOrigin
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserServer userServer;

    //    添加
    @Secured("ROLE_ADMIN")
    @PostMapping
    R add(@Valid @RequestBody LoginPara para) {

        userServer.add(para);

        return new R();
    }

    //    删除
    @Secured("ROLE_ADMIN")
    @DeleteMapping("/{id}")
    R del(@PathVariable Long id) {

        userServer.del(id);

        return new R();
    }
    //    删除
    @Secured("ROLE_ADMIN")
    @PutMapping("/enable/{id}")
    R enable(@PathVariable Long id) {

        userServer.enable(id);

        return new R();
    }
    //    权限管理
    //    修改信息
    @Secured("ROLE_ADMIN")
    @PutMapping("/{id}")
    R update(@Valid @RequestBody UserUpdatePara para,@PathVariable Long id) {

        userServer.update(id,para);

        return new R();
    }

    //    浏览列表
    @Secured("ROLE_ADMIN")
    @GetMapping("/page")
    R<PageList<User>> page(
            Integer page,
            Integer size,
            @RequestParam(defaultValue = "2") Integer order
    ) {
        System.out.println(String.format("page%s size%s", page.toString(), size));
//        User user = authServer.getUserByUsername(userDetails.getUsername());
        return new R<>() {{
            setData(userServer.page(page, size, order));
        }};
    }



}
